package com.foreveredu.erlms.web.struts.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.erlms.domain.Clazz;
import com.foreveredu.erlms.domain.Department;
import com.foreveredu.erlms.service.IManagementService;
import com.foreveredu.erlms.web.struts.actionform.ClazzActionForm;

public class ClazzAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 完成班级添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ClazzActionForm clazzForm = (ClazzActionForm) form;
		Clazz clazz = new Clazz();
		PropertyUtils.copyProperties(clazz, clazzForm);
		
		Department department = managementService.findDepartmentById(clazzForm.getDeptid());
		int deptId = department.getId();
		
		// 判断当前输入的院系编号是否合法！
		if (clazzForm.getClazzId() == null || "".equals(clazzForm.getClazzId())) {

			request.setAttribute("clazz", clazz);
			request.setAttribute("clazzError", "请输入班级编号！");
			request.setAttribute("deptId", deptId);
			request.setAttribute("UpdateClazz", "add");
			return mapping.findForward("toClassList");
		} else if (managementService.findClazzByClazzId(clazz.getClazzId()) != null) {
			request.setAttribute("clazz", clazz);
			request.setAttribute("clazzIdRequest", "该班级号已存在，请重新输入！");
			request.setAttribute("deptId", deptId);
			request.setAttribute("UpdateClazz", "add");
			return mapping.findForward("toClassList");
		}
		//班级编号可以使用中文名称
//		if(!Pattern.matches("^[0-9]+$", clazz.getClazzId())){
//			request.setAttribute("clazz", clazz);
//			request.setAttribute("clazzIdError", "院系编号必须为数字组成！");
//			request.setAttribute("deptId", deptId);
//			request.setAttribute("UpdateClazz", "add");
//			return mapping.findForward("toClassList");
//		}

		if (clazz.getClazzName() == null || "".equals(clazz.getClazzName())) {

			request.setAttribute("clazz", clazz);
			request.setAttribute("clazzNameError", "请输入院系名称！");
			request.setAttribute("deptId", deptId);
			request.setAttribute("UpdateClazz", "add");
			return mapping.findForward("toClassList");
		}
		
		clazz.setDept(department);
		managementService.saveClazz(clazz);
		
		request.setAttribute("deptId", deptId);

		return mapping.findForward("toClassList");
	}

	// 跳转到部门修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ClazzActionForm clazzfForm = (ClazzActionForm) form;
		Clazz clazz = managementService.findClazzById(clazzfForm.getId());
		request.setAttribute("clazz", clazz);
		
		List<Clazz> clazzList = managementService.findClazzsByDept(clazzfForm.getDeptid());
		request.setAttribute("clazzList", clazzList);

		Department dept = managementService.findDepartmentById(clazzfForm.getDeptid());
		request.setAttribute("dept", dept);
		
		request.setAttribute("UpdateClazz", "update");
		
		return mapping.findForward("toUpdateClazz");
	}

	// 保存部门修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ClazzActionForm clazzf = (ClazzActionForm) form;
		
		List<Clazz> clazzList = managementService.findClazzsByDept(clazzf.getDeptid());
		request.setAttribute("clazzList", clazzList);
		
		Department dept = managementService.findDepartmentById(clazzf.getDeptid());
		int deptId = dept.getId();
		request.setAttribute("dept", dept);

		// 判断当前输入的院系编号是否合法！
		if (!clazzf.getClazzId().equals(
				managementService.findClazzById(clazzf.getId()).getClazzId())) { // 判断提交的院系编号是否被用户修改

			if (clazzf.getClazzId() == null || "".equals(clazzf.getClazzId())) {

				Clazz clazz = managementService.findClazzById(clazzf
						.getId());
				request.setAttribute("clazz", clazz);

				request.setAttribute("clazzError", "请输入班级编号！");
				request.setAttribute("UpdateClazz", "update");
				return mapping.findForward("toUpdateClazz");
			} else if (managementService
					.findClazzByClazzId(clazzf.getClazzId()) != null) {

				Clazz clazz = managementService.findClazzById(clazzf
						.getId());
				request.setAttribute("clazz", clazz);

				request.setAttribute("clazzIdRequest", "该编号已存在，请重新输入！");
				request.setAttribute("UpdateClazz", "update");
				return mapping.findForward("toUpdateClazz");
			}
		}
		
		if(!Pattern.matches("^[0-9]+$", clazzf.getClazzId())){
			
			Clazz clazz = managementService.findClazzById(clazzf.getId());
			request.setAttribute("clazz", clazz);
			request.setAttribute("clazzIdError", "班级编号必须为数字组成！");
			request.setAttribute("UpdateClazz", "update");
			return mapping.findForward("toUpdateClazz");
		}
		if (clazzf.getClazzName() == null || "".equals(clazzf.getClazzName())) {
			Clazz clazz = managementService.findClazzById(clazzf.getId());
			request.setAttribute("clazz", clazz);

			request.setAttribute("clazzNameError", "请输入班级名称！");
			request.setAttribute("UpdateClazz", "update");
			return mapping.findForward("toUpdateClazz");
		}

		Clazz clazz = managementService.findClazzById(clazzf.getId());
		clazz.setClazzId(clazzf.getClazzId());
		clazz.setClazzName(clazzf.getClazzName());
		managementService.updateClazz(clazz);
		
		request.setAttribute("deptId", deptId);
		request.setAttribute("UpdateClazz", "add");
		return mapping.findForward("toClassList");
	}

	// 完成部门删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ClazzActionForm clazz = (ClazzActionForm) form;
		managementService.deleteClazzById(clazz.getId());
		
		Department department = managementService.findDepartmentById(clazz.getDeptid());
		int deptId = department.getId();
		request.setAttribute("deptId", deptId);
		
		return mapping.findForward("toClassList");
	}
}
